package com.xi.demo.controller;

import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/deepSeek")
public class DeepSeek {

    @Autowired
    private OpenAiChatModel openAiChatModel;



///deepSeek/chat
    @RequestMapping("/chat")
    public String chat(String message) {

        System.out.println(message);

        return openAiChatModel.call(message);
    }

    @RequestMapping("/callByPrompt")
    public String callByPrompt(String message) {
        ChatResponse call = openAiChatModel.call(
                new Prompt(new SystemMessage("你是小舒的专属答疑工具！"),
                        new UserMessage(message)));
        return call.getResult().getOutput().getText();
    }

    @GetMapping(value = "/callByPrompt2",produces = "text/html;charset=utf-8")
    public Flux<String>  callByPrompt2(String message) {
        Flux<ChatResponse> stream = openAiChatModel
                .stream(new Prompt(new SystemMessage("你是小舒的专属答疑工具！"),
                        new UserMessage(message)));

        return stream.map(x -> (x.getResult().getOutput().getText()));
    }

    @GetMapping(value = "/callByStream", produces = "text/html;charset=utf-8")
    public Flux<String> callByStream(String message) {
        Flux<ChatResponse> response = openAiChatModel.stream(new Prompt(message));
        return response.map(x->x.getResult().getOutput().getText());
    }



}
